package com.mgq.juc.threadpool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @author MaGuangQi
 * CachedThreadPool特点:
 * 1.没有核心线程数
 * 2.全部都是救急线程
 * 3.最大会创建Integer,MAX_VALUE线程数
 * 4.使用的是SynchronousQueue (没有容量) 一手交钱,一手交货
 * 5.救急线程默认存活1分钟,到期后会销毁
 * @date 2021-12-22 09:44
 **/
@Slf4j
public class TestJdkCachedThreadPool {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<String> future=executorService.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(5000);
                log.debug("执行任务1");
                return "1";
            }
        });
        Future<String> future2=executorService.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(1000);
                log.debug("执行任务2");
                return "2";
            }
        });
        Future<String> future3=executorService.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(1000);
                log.debug("执行任务3");
                return "3";
            }
        });
        try {
            log.debug(future2.get());
            log.debug(future.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}
